//
//  UIView+ICAnimation.m
//  ICome
//
//  Created by chenqs on 2017/7/10.
//  Copyright © 2017年 iCom. All rights reserved.
//

#import "UIView+ICAnimation.h"

@implementation UIView (ICAnimation)

- (void)addAnimation:(CGPoint)point{
    
    CGPoint centerPoint = point;

    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(centerPoint.x, centerPoint.y, 30, 30)];
    textLabel.text = [NSString stringWithFormat:@"+1"];
    textLabel.backgroundColor = [UIColor clearColor];
    textLabel.textColor = [UIColor redColor];

    textLabel.transform = CGAffineTransformMakeScale(0.2, 0.2);
    [self addSubview:textLabel];
    [self bringSubviewToFront:textLabel];
    
    //    CGFloat duration = 3 + (arc4random() % 5 - 2);
    CGFloat duration = 3 + (arc4random() % 3);
    [UIView animateWithDuration:0.15 animations:^{
        textLabel.transform = CGAffineTransformMakeScale(1, 1);
        textLabel.transform = CGAffineTransformMakeRotation(-0.01 * (arc4random() % 20));
    }];
    [UIView animateWithDuration:duration animations:^{
        textLabel.alpha = 0;
    }];
    CAKeyframeAnimation *animation = [[self class] createAnimation:textLabel.frame];
    animation.duration = duration;
    [textLabel.layer addAnimation:animation forKey:@"position"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((duration + 0.5) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [textLabel removeFromSuperview];
    });
}


+ (CAKeyframeAnimation *)createAnimation:(CGRect)frame
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    //    animation.additive = YES;
    CGMutablePathRef path = CGPathCreateMutable();
    
    int height = -80 + arc4random() % 20;
    int xOffset = frame.origin.x;
    int yOffset = frame.origin.y;
    int waveWidth = 6.;
    CGPoint p1 = CGPointMake(xOffset, height * 0 + yOffset);
    CGPoint p2 = CGPointMake(xOffset, height * 1 + yOffset);
    //CGPoint p3 = CGPointMake(xOffset, height * 2 + yOffset);
    //CGPoint p4 = CGPointMake(xOffset, height * 3 + yOffset);
    
    CGPathMoveToPoint(path, NULL, p1.x,p1.y);
    
    if (arc4random() % 2) {
        CGPathAddQuadCurveToPoint(path, NULL, p1.x - arc4random() % waveWidth, p1.y + height / 2.0, p2.x, p2.y);
       // CGPathAddQuadCurveToPoint(path, NULL, p2.x + arc4random() % waveWidth, p2.y + height / 2.0, p3.x, p3.y);
        //CGPathAddQuadCurveToPoint(path, NULL, p3.x - arc4random() % waveWidth, p3.y + height / 2.0, p4.x, p4.y);
    } else {
        CGPathAddQuadCurveToPoint(path, NULL, p1.x + arc4random() % waveWidth, p1.y + height / 2.0, p2.x, p2.y);
        //CGPathAddQuadCurveToPoint(path, NULL, p2.x - arc4random() % waveWidth, p2.y + height / 2.0, p3.x, p3.y);
        //CGPathAddQuadCurveToPoint(path, NULL, p3.x + arc4random() % waveWidth, p3.y + height / 2.0, p4.x, p4.y);
    }
    animation.path = path;
    animation.calculationMode = kCAAnimationCubicPaced;
    CGPathRelease(path);
    return animation;
}


- (void)showAnimationView:(id)view
{
    UIView *realView = (UIView *)view;
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    animation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.2, 0.2, 1)],
                         [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.05, 1.05, 1)],
                         [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1)]];
    animation.keyTimes = @[ @0, @0.5, @1 ];
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    animation.duration = .3;    
    [realView.layer addAnimation:animation forKey:@"showAlert"];
}

- (void)dismissAnimationView:(id)view
{
    UIView *realView = (UIView *)view;
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    animation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1)],
                         [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.6, 0.6, 1)],
                         [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.2, 0.2, 1)]];
    animation.keyTimes = @[ @0, @0.5, @1 ];
    animation.fillMode = kCAFillModeRemoved;
    animation.duration = .3;
    [realView.layer addAnimation:animation forKey:@"dismissAlert"];
}

- (void)circleAnimationWithView:(id)view
{
    UIView *realView = (UIView *)view;
    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"transform.rotation";
    anim.toValue = @(M_PI*2);
    anim.duration = 3;
    anim.repeatCount = MAXFLOAT;
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    [realView.layer addAnimation:anim forKey:nil];
}


- (void)commitcircleAnimationsWithView:(id)view
{
    UIView *realView = (UIView *)view;
    [realView.layer removeAllAnimations];
}
@end
